<?php


class Files extends Admin_controller
{
	
	public function __construct()
	{
		parent::__construct();
		$this->load->model( 'files_mdl', 'files');
	}
	
	
	public function index()
	{
		$files = array();
		$files = $this->files->getFilesList( false );
		
		$data['files'] = $files;
		$this->load->view( 'adminroot/files', $data );
		
	}
	
	/**
	 * 添加
	 */
	
	public function add()
	{
		
			$this->_load_validation_rules();
		if ( $this->form_validation->run() == FALSE ) {
					
			$this->load->view( 'adminroot/file_add' );
			
		} else {
			
			$data['filename']      = $this->input->post( 'filename' );
			$data['parentid']   = $this->input->post( 'parentid' );
			$data['shouquan']       = $this->input->post( 'shouquan' );
			$data['jiage']      = $this->input->post( 'jiage' );
			$data['tujianzhishu']    = $this->input->post( 'tujianzhishu' );
			$data['fenbianlv']     = $this->input->post( 'fenbianlv' );
			$data['chicun'] = $this->input->post( 'chicun' );
			$data['createtime'] = time();
			$data['geshi'] = $this->input->post( 'geshi' );
			$data['yanshemoshi'] = $this->input->post( 'yanshemoshi' );
			$data['gongju'] = $this->input->post('gongju');
			$data['filenum'] = date('YmdHis').time();
			
			$updata = array();
			//文件上传
			if(!empty($_FILES['filesmallimg'])){
				
				$config['upload_path'] = './upload/smallpic/';
				$config['allowed_types'] = 'gif|jpg|png';
				$config['encrypt_name'] = true;
				$config['overwrite'] = false;
			  	$this->load->library('upload', $config);
 
				  if ( ! $this->upload->do_upload('filesmallimg'))
				  {
				   $error = array('error' => $this->upload->display_errors());
				  	var_dump($error);
				  }
				  $updata = $this->upload->data();
				  
			}
			
			$updata2 = array();
			//附件上传
			if(!empty($_FILES['downfile'])){
				
				$config['upload_path'] = './upload/src/';
				$config['allowed_types'] = 'gif|jpg|png';
				$config['encrypt_name'] = true;
				$config['overwrite'] = false;
			  	$this->load->library('upload', $config);
 
				  if ( ! $this->upload->do_upload('downfile'))
				  {
				   $error = array('error' => $this->upload->display_errors());
				  	var_dump($error);
				  }
				  $updata2 = $this->upload->data();
			}
			
			$data['filesmallimg'] = $updata['file_name'];
			$data['filedir'] = $updata2['file_name'];
			$dat['daxiao'] = $updata2['file_size'];
			
			if ( $this->files->addFile( $data) ) {
				
				redirect( 'adminroot/files' );
				
			}		
		}
		
	}
	
	
	/**
	 * 
	 * 表单验证
	 */
	private function _load_validation_rules()
	{
		$this->form_validation->set_error_delimiters('<li>', '</li>');
		$this->form_validation->set_rules('filename','标题','required|xss_clean');

	}
	
}